package org.jetnuke.dao.generic;

import org.apache.log4j.Logger;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import net.jforum.JForumExecutionContext;
import net.jforum.dao.generic.AutoKeys;
import net.jforum.util.preferences.SystemGlobals;

import org.jetnuke.entities.DownloadsVoteData;

public class GenericDownloadVoteDataDAO extends AutoKeys implements
		org.jetnuke.dao.DownloadVoteDataDAO {
	/**
	 * Logger for this class
	 */
	private static final Logger logger = Logger
			.getLogger(GenericDownloadVoteDataDAO.class);

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.jetnuke.model.DownloadVoteData#retrieveByPK(int)
	 */
	public DownloadsVoteData retrieveByPK(int id) throws Exception {
		if (logger.isDebugEnabled()) {
			logger.debug("retrieveByPK(int) - start"); //$NON-NLS-1$
		}

		PreparedStatement p = JForumExecutionContext.getConnection()
				.prepareStatement(
						SystemGlobals
								.getSql("DownloadVoteDataModel.selectByID"));
		p.setInt(1, id);

		ResultSet rs = p.executeQuery();
		DownloadsVoteData s = new DownloadsVoteData();

		if (rs.next()) {
			fillDownVoteDataFromResultSet(s, rs);
		}

		rs.close();
		p.close();

		if (logger.isDebugEnabled()) {
			logger.debug("retrieveByPK(int) - end"); //$NON-NLS-1$
		}
		return s;
	}

	/**
	 * @param s
	 * @param rs
	 */
	private void fillDownVoteDataFromResultSet(DownloadsVoteData s, ResultSet rs)
			throws Exception {
		if (logger.isDebugEnabled()) {
			logger
					.debug("fillDownVoteDataFromResultSet(DownloadsVoteData, ResultSet) - start"); //$NON-NLS-1$
		}

		s.setRATING(rs.getInt("RATING"));
		s.setRATINGCOMMENTS(rs.getString("RATINGCOMMENTS"));
		s.setRATINGDBID(rs.getInt("RATINGDBID"));
		s.setRATINGHOSTNAME(rs.getString("RATINGHOSTNAME"));
		s.setRATINGLID(rs.getInt("RATINGLID"));
		s.setRATINGTIMESTAMP(rs.getTimestamp("RATINGTIMESTAMP"));
		s.setRATINGUSER(rs.getString("RATINGUSER"));

		if (logger.isDebugEnabled()) {
			logger
					.debug("fillDownVoteDataFromResultSet(DownloadsVoteData, ResultSet) - end"); //$NON-NLS-1$
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.jetnuke.model.DownloadVoteData#addNew(org.jetnuke.entities.DownloadsVoteData)
	 */
	public int addNew(DownloadsVoteData dc) throws Exception {
		if (logger.isDebugEnabled()) {
			logger.debug("addNew(DownloadsVoteData) - start"); //$NON-NLS-1$
		}

		PreparedStatement p = this
				.getStatementForAutoKeys("DownloadVoteDataModel.addNew");
		this.setAutoGeneratedKeysQuery(SystemGlobals
				.getSql("DownloadVoteDataModel.lastGeneratedVoteDataID"));
		initNewDownloadVoteData(dc, p);

		int id = this.executeAutoKeysQuery(p);
		p.close();

		if (logger.isDebugEnabled()) {
			logger.debug("addNew(DownloadsVoteData) - end"); //$NON-NLS-1$
		}
		return id;
	}

	/**
	 * @param dc
	 * @param p
	 */
	private void initNewDownloadVoteData(DownloadsVoteData dc,
			PreparedStatement p) throws Exception {
		if (logger.isDebugEnabled()) {
			logger
					.debug("initNewDownloadVoteData(DownloadsVoteData, PreparedStatement) - start"); //$NON-NLS-1$
		}

		p.setInt(1, dc.getRATING());
		p.setString(2, dc.getRATINGCOMMENTS());
		p.setString(3, dc.getRATINGHOSTNAME());
		p.setInt(4, dc.getRATINGLID());
		p.setTimestamp(5, new Timestamp(dc.getRATINGTIMESTAMP().getTime()));
		p.setString(6, dc.getRATINGUSER());

		if (logger.isDebugEnabled()) {
			logger
					.debug("initNewDownloadVoteData(DownloadsVoteData, PreparedStatement) - end"); //$NON-NLS-1$
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.jetnuke.model.DownloadVoteData#countAnonymous(int,
	 *      java.lang.String, java.util.Date)
	 */
	public int countAnonymous(int lid, String hostName, Date dt)
			throws Exception {
		if (logger.isDebugEnabled()) {
			logger.debug("countAnonymous(int, String, Date) - start"); //$NON-NLS-1$
		}

		PreparedStatement p;

		p = JForumExecutionContext.getConnection().prepareStatement(
				SystemGlobals.getSql("DownloadVoteDataModel.countAnonymous"));
		p.setInt(1, lid);
		p.setString(2, hostName);
		p.setTimestamp(3, new Timestamp(dt.getTime()));
		ResultSet rs = p.executeQuery();
		rs.next();

		int r = rs.getInt(1);
		rs.close();
		p.close();

		if (logger.isDebugEnabled()) {
			logger.debug("countAnonymous(int, String, Date) - end"); //$NON-NLS-1$
		}
		return r;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.jetnuke.model.DownloadVoteData#getByRatingLid(int)
	 */
	public List getByRatingLid(int lid) throws Exception {
		if (logger.isDebugEnabled()) {
			logger.debug("getByRatingLid(int) - start"); //$NON-NLS-1$
		}

		PreparedStatement p;

		p = JForumExecutionContext
				.getConnection()
				.prepareStatement(
						SystemGlobals
								.getSql("DownloadVoteDataModel.selectByRatingLid"));
		p.setInt(1, lid);

		ResultSet rs = p.executeQuery();
		List list = this.processSelectAll(rs);
		rs.close();
		p.close();

		if (logger.isDebugEnabled()) {
			logger.debug("getByRatingLid(int) - end"); //$NON-NLS-1$
		}
		return list;
	}

	protected List processSelectAll(ResultSet rs) throws Exception {
		if (logger.isDebugEnabled()) {
			logger.debug("processSelectAll(ResultSet) - start"); //$NON-NLS-1$
		}

		List list = new ArrayList();
		while (rs.next()) {
			DownloadsVoteData u = new DownloadsVoteData();
			fillDownVoteDataFromResultSet(u, rs);
			list.add(u);
		}

		if (logger.isDebugEnabled()) {
			logger.debug("processSelectAll(ResultSet) - end"); //$NON-NLS-1$
		}
		return list;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.jetnuke.model.DownloadVoteData#countUser(int, java.lang.String)
	 */
	public int countUser(int lid, String uname) throws Exception {
		if (logger.isDebugEnabled()) {
			logger.debug("countUser(int, String) - start"); //$NON-NLS-1$
		}

		PreparedStatement p;

		p = JForumExecutionContext.getConnection().prepareStatement(
				SystemGlobals.getSql("DownloadVoteDataModel.countUser"));
		p.setInt(1, lid);
		p.setString(2, uname);
		ResultSet rs = p.executeQuery();
		rs.next();

		int r = rs.getInt(1);
		rs.close();
		p.close();

		if (logger.isDebugEnabled()) {
			logger.debug("countUser(int, String) - end"); //$NON-NLS-1$
		}
		return r;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.jetnuke.model.DownloadVoteDataModel#selectWithComment(int)
	 */
	public List selectWithComment(int lid) throws Exception {
		if (logger.isDebugEnabled()) {
			logger.debug("selectWithComment(int) - start"); //$NON-NLS-1$
		}

		PreparedStatement p;

		p = JForumExecutionContext
				.getConnection()
				.prepareStatement(
						SystemGlobals
								.getSql("DownloadVoteDataModel.selectWithComment"));
		p.setInt(1, lid);

		ResultSet rs = p.executeQuery();
		List list = this.processSelectAll(rs);
		rs.close();
		p.close();

		if (logger.isDebugEnabled()) {
			logger.debug("selectWithComment(int) - end"); //$NON-NLS-1$
		}
		return list;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.jetnuke.model.DownloadVoteDataModel#getByRatingUser(int)
	 */
	public List getByRatingUser(String user) throws Exception {
		if (logger.isDebugEnabled()) {
			logger.debug("getByRatingUser(String) - start"); //$NON-NLS-1$
		}

		PreparedStatement p = JForumExecutionContext
				.getConnection()
				.prepareStatement(
						SystemGlobals
								.getSql("DownloadVoteDataModel.selectByRatingUser"));
		p.setString(1, user);

		ResultSet rs = p.executeQuery();
		List list = this.processSelectAll(rs);
		rs.close();
		p.close();

		if (logger.isDebugEnabled()) {
			logger.debug("getByRatingUser(String) - end"); //$NON-NLS-1$
		}
		return list;
	}

}
